package mobileann.mafamily.utils;

import java.util.HashMap;
import java.util.Map;

import mobileann.mafamily.entity.RefreshInterfaceEntity;

import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.mofind.android.net.HttpUtils;
import com.mofind.android.net.HttpUtils.HttpResultCallback;
import com.mofind.android.net.JSONParser;

public class RefreshNotifyUtils {
	private static RefreshNotifyUtils _instance;
	public static final String COMMEND="commend";
	public static final String FENCE="fence";
	public static final String POLICY="policy";
	public static final String MEMBER="member";
	public static final String DEVICE="device";
	public static final String LOGIN="login";
	public static final int REFRESH_SUCCESS=791;
	public static final int ADDNOTICE_SUCCESS=792;
	public static final int ADDREFEREE_SUCCESS=793;
	public static final int ADDREFEREE_ERROR=794;
	
	public static final int DO_REFRESH = 795;

	public synchronized static RefreshNotifyUtils getInstance() {
		if (_instance == null) {
			_instance = new RefreshNotifyUtils();
		}
		return _instance;
	}
	
	public void getRefreshInterface(final String uid,final String urad,final Handler handler){
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET+uid+"_"+urad+"_notice", new HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				// TODO Auto-generated method stub
				if(isResult){
					String json1=TripleDESUtil.CBCDecode(uid, urad, jsonStr);
					if(!TextUtils.isEmpty(json1)){
						int end=json1.lastIndexOf("}");
						String json=json1.substring(0, end+1);
						JSONObject jo=JSONParser.getJSONObject(json);
						String status=JSONParser.getString(jo, "status");
						if(status==null){
							int commend=JSONParser.getInt(jo, COMMEND);
							int fence=JSONParser.getInt(jo, FENCE);
							int policy=JSONParser.getInt(jo, POLICY);
							int member=JSONParser.getInt(jo, MEMBER);
							int device=JSONParser.getInt(jo, DEVICE);
							int login=JSONParser.getInt(jo, LOGIN);
//							Log.i("Status", commend + "::"+fence + "::"+policy + "::"+member + "::"+device + "::"+login);
							SPUtils.setShareCoin(uid, commend);
							RefreshInterfaceEntity entity=new RefreshInterfaceEntity(commend, fence, policy, member, device, login);
							if(handler!=null){
								handler.obtainMessage(REFRESH_SUCCESS, entity).sendToTarget();
							}
						}else{
							//获取信息失败
							SPUtils.setShareCoin(uid, 0);
						}
					}
				}
			}
		});
	}
	
	private Map<String, String> getAddNoticePara(String uid,String urad,String mode){
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "add_notice");
		map.put("uid", uid);
		map.put("data", TripleDESUtil.CBCEncode(uid,urad,addNoticeData(uid, mode)));
		return map;
	}
	private String addNoticeData(String uid,String notice){
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("notice", notice);
		String data=MyTaskUtils.getInstance().map2json(map);
		return data;
	}
	public void doAddNotify(final String uid,final String urad,String mode,final Handler handler){
		HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getAddNoticePara(uid, urad, mode), new HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				// TODO Auto-generated method stub
				if(isResult){
					String json1=TripleDESUtil.CBCDecode(uid, urad, jsonStr);
					if(!TextUtils.isEmpty(json1)){
						int end=json1.lastIndexOf("}");
						String json=json1.substring(0, end+1);
						JSONObject jo=JSONParser.getJSONObject(json);
						String status=JSONParser.getString(jo, "status");
						if("true".equals(status)){
							if(handler!=null){
								handler.obtainMessage(ADDNOTICE_SUCCESS).sendToTarget();
							}
						}else{
							//上传刷新的接口失败
						}
					}
				}
			}
		});
	}
	private Map<String, String> getAddRefereePara(Context c,String uid,String urad,String referee_code){
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "add_referee");
		map.put("uid", uid);
		map.put("data", addData(c, uid, urad, referee_code));
		return map;
	}
	private String addData(Context c,String uid,String urad,String referee_code){
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "add_referee");
		map.put("uid", uid);
		map.put("referee_code", referee_code);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		String data = MyTaskUtils.getInstance().map2json(map);
		return TripleDESUtil.CBCEncode(uid,urad,data);
	}
	public void doAddReferee(Context c,final String uid,final String urad,String referee_code,final Handler handler){
		HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getAddRefereePara(c ,uid, urad, referee_code), new HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				// TODO Auto-generated method stub
				if(isResult){
					String json1=TripleDESUtil.CBCDecode(uid, urad, jsonStr);
					if(!TextUtils.isEmpty(json1)){
						int end=json1.lastIndexOf("}");
						String json=json1.substring(0, end+1);
						JSONObject jo=JSONParser.getJSONObject(json);
						String status=JSONParser.getString(jo, "status");
						if("true".equals(status)){
							if(handler!=null){
								handler.obtainMessage(ADDREFEREE_SUCCESS).sendToTarget();
							}
						}else{
							String msg=JSONParser.getString(jo, "msg");
							if(handler!=null){
								handler.obtainMessage(ADDREFEREE_ERROR, msg).sendToTarget();
							}
						}
					}
				}
			}
		});
	}
	public void getReferee(final String uid,final String urad){
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET+uid+"_"+urad+"_referee", new HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				// TODO Auto-generated method stub
				if(isResult){
					String json1=TripleDESUtil.CBCDecode(uid, urad, jsonStr);
					if(!TextUtils.isEmpty(json1)){
						int end=json1.lastIndexOf("}");
						String json=json1.substring(0, end+1);
						JSONObject jo=JSONParser.getJSONObject(json);
						String status=JSONParser.getString(jo, "status");
						if(TextUtils.isEmpty(status)){
							String code=JSONParser.getString(jo, "code");
							SPUtils.setRefereeCode(uid, code);
						}
					}
				}
			}
		});
	}
}
